/****************************************************************************
* @file    GPIO/IOToggle/main.c 
* @author  MCD Application Team
* @version V3.5.0
* @date    08-April-2011
* @brief   Main program body.
*****************************************************************************/ 

#include "SysTick.h"
#include "LedDriver.h"
#include "Reader.h"
#include "Rtc.h"
#include "TcpIp.h"
#include "AT25DF041.h"
#include "Config.h"
#include "Counter.h"
#include "User.h"
#include "StrLib.h"

//#define AUTO_REGISTER_CARD

int main(void)
{
    U32 cardCode;
    U16 timeZone = 0;
    U16 userID;
    U8  userCount = 0;

    cCounter::InitReset();

    // initialize system
    for (U32 i=0; i<500000; ++i);
    cSysTick::InitReset();
    cLedDriver::InitReset();
    cRtc::InitReset();
    cFlash::InitReset();
    cConfig::InitReset();

    cCounter::LoadConfig();
    cReader::InitReset();
    cUser::InitReset();

    cTcpIp::InitReset();


    // run main loop
    while (1)
    {
        cLedDriver::RunOne();
        cRtc::RunOne();
        cCounter::RunOne();
        cTcpIp::RunOne();
        cReader::RunOne();
        cardCode = cReader::GetNewCard();
        if (0 != cardCode)
        {
#if !defined AUTO_REGISTER_CARD
            bool useMatch = cUser::FindUser(cardCode, timeZone, userID);
            U16  timeZoneStatus = cCounter::GetTimeZoneStatus();
            //if ((cardCode != cardOld) && (true == cUser::FindUser(cardCode, timeZone)))
            if ((true == useMatch) && (0 != (timeZone & timeZoneStatus)))
            {
                if (false == cUser::IsUserRead(userID))
                {
                    cCounter::SetLedGreen(true);
                    cCounter::SetLedRed(false);
                    cCounter::SetBeep(false);

                    cCounter::IncCounter();
                    cUser::SetUserRead(userID);
                }
                else
                {
                    cCounter::SetLedRed(true);
                    cCounter::SetBeep(false);
                    cCounter::SetLedGreen(true);
                }
            }
            else
            {
                cCounter::SetLedRed(true);
                cCounter::SetBeep(false);
                cCounter::SetLedGreen(false);
            }
            //cardOld = cardCode;
#else
            if (0 == userCount)
            {
                cUser::EraseAll();
                U16 timeZoneStart[16];
                U16 timeZoneEnd[16];

                U8 *pStrTime;
                U32 currMinute;
                pStrTime = cRtc::GetTimeAsStr();
                currMinute = pStrTime[3]*60 + pStrTime[4];

                currMinute += 2;
                for (U8 i=0; i<16; i++)
                {
                    timeZoneStart[i] = currMinute + i*2;
                }
                for (U8 i=0; i<16; i++)
                {
                    timeZoneEnd[i] = currMinute + i*2 + 1;
                }
                cConfig::SetTimeZone(timeZoneStart, timeZoneEnd);
            }
            timeZone = 1<<userCount;
            cUser::Register(cardCode, timeZone);
            cCounter::SetLedGreen(true);

            ++userCount;
#endif
        }
    }
}
